/**
 * 考試管理初始化
 */
var Testing = {
    id: "TestingTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    TestingData: {},
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Testing.initColumn = function () {
    return [
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle', hidden: true},
        {
            title: 'testQuestionsId',
            field: 'testQuestionsId',
            visible: false,
            align: 'center',
            valign: 'middle',
            hidden: true
        },
        {
            title: 'correctAnswer',
            field: 'correctAnswer',
            visible: false,
            align: 'center',
            valign: 'middle',
            hidden: true
        },
        {title: 'userAnswer', field: 'userAnswer', visible: false, align: 'center', valign: 'middle', hidden: true},
        {title: 'testNumber', field: 'testNumber', visible: false, align: 'center', valign: 'middle', hidden: true},
        {
            title: '', field: 'testTitle', align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                if (row.userAnswer == undefined) {
                    row.userAnswer = "";
                }
                return row.testNumber + "." + value + "   (" + " " + row.userAnswer + " " + ")";
            }
        },
        {title: 'A：', field: 'optionsA', align: 'center', valign: 'middle',},
        {title: 'B：', field: 'optionsB', align: 'center', valign: 'middle',},
        {title: 'C：', field: 'optionsC', align: 'center', valign: 'middle',},
        {title: 'D：', field: 'optionsD', align: 'center', valign: 'middle',},
        {title: 'E：', field: 'optionsE', align: 'center', valign: 'middle',},
        {title: 'F：', field: 'optionsF', align: 'center', valign: 'middle',},
        {
            title: '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  ',
            field: '', align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                return '<a onclick="Testing.chooseAnswer(\'' + row.id + '\');" href="javascript:;" >请选择</a>';
            }
        }
    ];
};

$(document).scroll(function (e) {
    if ($(document).scrollTop() != 0) {
        $("#test2").stop();
        $("#test2").animate({"top": "55"});
    }
    else {
        $("#test2").stop();
        $("#test2").animate({"top": "110px"});
    }
});
$(document).ready(function () {
    window.onload = function () {
        var timeLength = $("#timeLength").val();
        var totleSecond = timeLength * 60;
        var totleSecond = initTimer();
        startTime(totleSecond);

        function startTime(totleSecond) {
            document.getElementById('time').value = formatTime(totleSecond);
            var timer = setInterval(function () {
                totleSecond -= 1;
                document.getElementById('remainingTime').value = totleSecond;
                if (totleSecond >= 0) {
                    document.getElementById('time').value = formatTime(totleSecond);
                    times.innerHTML = "倒计时:" + formatTime(totleSecond);
                } else {
                    Testing.handOverAnswei();
                    alert("考试结束！");
                    clearInterval(timer);
                    document.getElementById('remainingTime').value = "";
                }
            }, 1000)
        }
    }

    function initTimer() {
        var totleSecond = Number($("#remainingTime").val());
        return totleSecond;
    }

    function formatTime(totleSecond) {
        var hour = Math.floor(totleSecond / 3600);
        var minute = Math.floor((totleSecond % 3600) / 60);
        second = Math.floor(totleSecond % 60);
        hour = formatTimeNumber(hour);
        minute = formatTimeNumber(minute)
        second = formatTimeNumber(second);
        return (hour + ":" + minute + ":" + second)
    }

    function formatTimeNumber(number) {
        if (number < 10) {
            return "0" + number;
        } else {
            return number;
        }
    }
})
Testing.chooseAnswer = function (id) {
    var index = layer.open({
        type: 2,
        title: '选择答案',
        area: ['50%', '45%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/testing/answerList/' + id
    });
    this.layerIndex = index;
};

/**
 * 检查是否选中
 */
Testing.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Testing.seItem = selected[0];
        return true;
    }
};

Testing.getMoney = function (url) {
    window.location.href = url;
}
/**
 * 清除数据
 */
Testing.clearData = function () {
    this.TestingData = {};
}

Testing.collectData = function () {
    this.set('theTestId').set('userId');
}
Testing.set = function (key, val) {
    this.TestingData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}
Testing.tiaoz = function () {
    $("ul").toggle();
}

/**
 * 交卷
 */
Testing.getNullTitle = function () {
    this.clearData();
    this.collectData();
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/getTestGenerate/getNullTitle", function (data) {
        console.log(data);
        if (data.length > 0) {
            var msg = "您第" + data + "题没做？\n\n确认交卷吗！";
            if (confirm(msg) == true) {
                Testing.handOverAnswei();
                return true;
            } else {
                return false;
            }
        } else {
            Testing.handOverAnswei();
        }
    }, function (data) {
        Feng.error("交卷失败!内部故障" + data.responseJSON.message + "!");
    });
    ajax.set(this.TestingData);
    ajax.start();
}


/**
 * 交卷
 */
Testing.handOverAnswei = function () {
    this.clearData();
    this.collectData();
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/getTestGenerate/handOverAnswei", function (data) {
        if (data == "redirect:/testMenu") {
            alert("已交卷!");
        } else {
            alert("交卷成功!考试分数：" + data + "");
        }
        window.location.href = Feng.ctxPath + "/testMenu";
    }, function (data) {
        Feng.error("交卷失败!内部故障" + data.responseJSON.message + "!");
    });
    ajax.set(this.TestingData);
    ajax.start();
}
/**
 * 交卷
 */
Testing.handOverMoni = function () {
    this.clearData();
    this.collectData();
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/getTestGenerate/handOverMoni", function (data) {
        if (data == "redirect:/testMenu") {
            alert("已交卷!");
        } else {
            alert("交卷成功!考试分数：" + data + "");
        }
        window.location.href = Feng.ctxPath + "/testMenu";
    }, function (data) {
        Feng.error("交卷失败!内部故障" + data.responseJSON.message + "!");
    });
    ajax.set(this.TestingData);
    ajax.start();
}
/**
 * 跳转指定页面
 */
Testing.jump = function (number) {
    myslider.slideTo(number);
}

Testing.resetSearch = function () {
    $("#theTestId").val("");
    $("#userId").val("");
    Testing.search();
};

Testing.formParams = function () {
    var queryData = {};
    queryData['theTestId'] = $("#theTestId").val();
    queryData['userId'] = $("#userId").val();
    return queryData;
}

var parseParam = function (param, key) {
    var paramStr = "";
    if (param instanceof String || param instanceof Number || param instanceof Boolean) {
        paramStr += "&" + key + "=" + param;
    } else {
        $.each(param, function (i) {
            var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);
            paramStr += '&' + parseParam(this, k);
        });
    }
    return paramStr.substr(1);
};

/**
 * 查询考試列表
 */
Testing.search = function () {
    Testing.table.refresh({query: Testing.formParams()});
};

$(function () {
    document.getElementById("theTestId").style.display = "none";
    document.getElementById("timeLength").style.display = "none";
    document.getElementById("theTestName").style.display = "none";
    document.getElementById("userName").style.display = "none";
    document.getElementById("userId").style.display = "none";
    var defaultColunms = Testing.initColumn();
    var table = new BSTable(Testing.id, "/getTestGenerate/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(Testing.formParams());
    // Testing.table = table.init();
    // brs.innerHTML = "</br></br></br>";
});
